package co.china.lottery.account.dao;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import co.china.lottery.account.dao.base.CommonDao;
import co.china.lottery.account.dao.entity.LPayAccountHis;
import co.china.lottery.account.dao.entity.LPrizeAccount;
import co.china.lottery.account.dao.entity.LPrizeAccountHis;
import co.china.lottery.account.util.Tools;

@Repository
public class PrizeAccountDao extends CommonDao {

	/**
	 * 保存
	 */
	@Transactional
	public void save(LPrizeAccount entity) {
		em.persist(entity);
	}

	/**
	 * 删除
	 */
	public void delete(LPrizeAccount entity) {
		em.remove(entity);
	}

	/**
	 * 更新
	 */
	public LPrizeAccount update(LPrizeAccount entity) {
		entity = em.merge(entity);
		return entity;
	}

	@SuppressWarnings("unchecked")
	public LPrizeAccount findAccountByAgentId(String agentId) {
		String sql = "SELECT t FROM LPrizeAccount t WHERE t.agentId = :agentId and status=:status order by createTime DESC ";
        Query query = em.createQuery(sql);
        query.setParameter("agentId", agentId);
        query.setParameter("status", LPrizeAccount.STATUS_VALID);
        query.setFirstResult(0);
        query.setMaxResults(1);
        List<LPrizeAccount> list = query.getResultList();
        if(Tools.isBlank(list)) {
            return null;
        }
        return list.get(0);
    }

	/**
	 * 保存
	 */
	@Transactional
	public void save(LPrizeAccountHis entity) {
		em.persist(entity);
	}

	public synchronized double unCommitHis(String agentId){
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT sum(debitCredit) FROM LPrizeAccountHis t WHERE 2>1  ");
		sb.append("	AND agentId =:agentId 	");
		sb.append("	AND status =:status 	");
		sb.append("	AND commitStatus =:commitStatus 	");
		Query query = em.createQuery(sb.toString());
		query.setParameter("agentId", agentId);
		query.setParameter("status", LPrizeAccountHis.STATUS_VALID);
		query.setParameter("commitStatus", LPrizeAccountHis.COMMIT_STATUS_NO);
		Object amount =   query.getSingleResult();
		if(amount==null){
			return 0.0;
		}
		return ((Double) amount).doubleValue();
	}
	
	public synchronized double unCommitHis(String agentId,String batchId){
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT sum(debitCredit) FROM LPrizeAccountHis t WHERE 2>1  ");
		sb.append("	AND agentId =:agentId 	");
		sb.append("	AND status =:status 	");
		sb.append("	AND commitStatus =:commitStatus 	");
		sb.append("	AND batchId =:batchId 	");
		Query query = em.createQuery(sb.toString());
		query.setParameter("agentId", agentId);
		query.setParameter("status", LPrizeAccountHis.STATUS_VALID);
		query.setParameter("commitStatus", LPrizeAccountHis.COMMIT_STATUS_NO);
		query.setParameter("batchId", batchId);
		Object amount =   query.getSingleResult();
		if(amount==null){
			return 0.0;
		}
		return ((Double) amount).doubleValue();
	}

	public int updateUnCollectBatch(String agentId, String batchId) {
		String sqlString = "UPDATE  L_PRIZE_ACCOUNT_HIS a set a.COMMIT_STATUS=:collectY " +
				"  WHERE AGENT_ID= :agentId and  STATUS=:status and BATCH_ID=:batchId and COMMIT_STATUS=:collectStatus ";
		Query q = em.createNativeQuery(sqlString);
		q.setParameter("agentId", agentId);
		q.setParameter("collectY", LPayAccountHis.COMMIT_STATUS_YES);
		q.setParameter("status", LPayAccountHis.STATUS_VALID);
		q.setParameter("collectStatus", LPayAccountHis.COMMIT_STATUS_NO);
		q.setParameter("batchId", batchId);
		int count = q.executeUpdate();
		return count;
	}
	
	@Transactional  
	public int updateLPrizeAccount(String agentId, Double balance) {
		StringBuffer sql=new StringBuffer("UPDATE  L_PRIZE_ACCOUNT  a set a.balance=");
					sql.append(balance).append(" WHERE a.AGENT_ID=").append(agentId)
					.append(" and a.STATUS=").append(LPayAccountHis.STATUS_VALID);
		Query q = em.createNativeQuery(sql.toString());
		int count=q.executeUpdate();
		return count;
	}
}